package cn.mumzone.pos.presenter;

import java.util.ArrayList;
import java.util.List;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.pos.model.InventoryGoodsEntity;
import cn.mumzone.pos.model.InventoryResultEntity;
import cn.mumzone.pos.model.source.StockRepository;
import cn.mumzone.pos.presenter.contract.InventoryResultContract;

/**
 * Created by qiuzhenhuan on 2019/10/12.
 */

public class InventoryResultPresenter extends AbsPresenter<InventoryResultContract.View, StockRepository> implements InventoryResultContract.Presenter {


    @Override
    public void sendInventoryResult(final String inventoryId, List<InventoryGoodsEntity> goodsList, String inventoryRemark) {

        ApiCallBack<Object> callBack = new ApiCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                doUpdateInventoryStatus(inventoryId, "3");
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };

        ArrayList<InventoryResultEntity> goodsBatchList = new ArrayList<>();

        for (InventoryGoodsEntity goodsEntity : goodsList) {
            for (InventoryGoodsEntity.GoodsBatchNumListBean bean : goodsEntity.getGoodsBatchNumList()) {
                InventoryResultEntity batchGoods = new InventoryResultEntity();
                batchGoods.setId(bean.getId());
                batchGoods.setGoodsNum(bean.getGoodsNum());
                batchGoods.setSkuNum(bean.getSkuNum());
                goodsBatchList.add(batchGoods);
            }
        }
        mRepo.updateInventoryNum(inventoryId, goodsBatchList, inventoryRemark, callBack);
        addSubscrebe(callBack);
    }


    public void doUpdateInventoryStatus(String inventoryId, String status) {
        ApiCallBack<Object> callBack = new ApiCallBack<Object>() {
            @Override
            public void onSuccess(Object detailEntity) {
                mView.showInventorySuccess();
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };
        mRepo.updateInventoryStatus(inventoryId, status, callBack);
        addSubscrebe(callBack);
    }
}
